﻿using Microsoft.Practices.Prism.StoreApps;
using Microsoft.Practices.Prism.StoreApps.Interfaces;
using Newtonsoft.Json;
using Sumc.Client.ModernUI.Common;
using Sumc.Client.ModernUI.Extensions;
using Sumc.Client.ModernUI.Helpers;
using Sumc.Client.ModernUI.Models;
using Sumc.Client.ModernUI.Services;
using Sumc.Models.Responses;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Windows.UI.Xaml.Navigation;

namespace Sumc.Client.ModernUI.ViewModels
{
    public class TransportPageViewModel : BaseViewModel
    {
        private string transportNumber;
        private TransportType transportType;
        private DelegateCommand<RouteStopInformation> goToScheduleInformation;
        private ObservableCollection<ScheduleDirection> directions;
        private readonly IScheduleRepository scheduleRepository;

        public TransportPageViewModel(INavigationService navigationService,
            IAlertMessageService alertMessageService,
            IScheduleRepository scheduleRepository,
            ISearchPaneService searchPaneService,
            IFlyoutService flyoutService)
            : base(navigationService, 
            alertMessageService,
            flyoutService,
            searchPaneService)
        {
            this.scheduleRepository = scheduleRepository;
        }

        [RestorableState]
        private ScheduleDirections ScheduleDirections { get; set; }

        public DelegateCommand<RouteStopInformation> GoToScheduleInformation
        {
            get
            {
                if (this.goToScheduleInformation == null)
                {
                    this.goToScheduleInformation = new DelegateCommand<RouteStopInformation>(this.NavigateToScheduleInformation);
                }

                return this.goToScheduleInformation;
            }
        }

        public ObservableCollection<ScheduleDirection> Directions
        {
            get
            {
                return this.directions;
            }

            set
            {
                this.SetProperty(ref this.directions, value);
            }
        }

        public string TransportNumber
        {
            get
            {
                return this.transportNumber;
            }

            set
            {
                this.SetProperty(ref this.transportNumber, value);
            }
        }

        public TransportType TransportType
        {
            get
            {
                return this.transportType;
            }

            set
            {
                this.SetProperty(ref this.transportType, value);
            }
        }

        private async void NavigateToScheduleInformation(RouteStopInformation routeStopInformation)
        {
            var scheduleInformation = new ScheduleInformation
            {
                Lid = this.ScheduleDirections.Lid,
                Rid = this.ScheduleDirections.Rid,
                TransportType = this.ScheduleDirections.TransportType,
                StopValue = routeStopInformation.Value
            };
            await this.navigationService.NavigateJson(Constants.SchedulePage, scheduleInformation);
        }

        private void LoadTransportDirections(TransportInformation transportInformation)
        {
            this.LoadData(async () =>
            {
                var scheduleDirections = await this.scheduleRepository.GetTransportDirections(transportInformation.Type, transportInformation.Title);
                this.LoadingData = false;
                this.SetData(scheduleDirections);
            });
        }
  
        private void SetData(ScheduleDirections scheduleDirections)
        {
            this.ScheduleDirections = scheduleDirections;
            this.TransportNumber = scheduleDirections.TransportNumber;
            this.TransportType = scheduleDirections.TransportType;
            this.Directions = new ObservableCollection<ScheduleDirection>();
            this.Directions.FillCollection(scheduleDirections.Directions);
        }

        public override async void OnNavigatedTo(object navigationParameter, NavigationMode navigationMode, Dictionary<string, object> viewModelState)
        {
            base.OnNavigatedTo(navigationParameter, navigationMode, viewModelState);
            var objectForDeserialize = navigationParameter as string;
            var scheduleDirections = await JsonConvert.DeserializeObjectAsync<ScheduleDirections>(objectForDeserialize);
            if (scheduleDirections != null && scheduleDirections.Lid != null)
            {
                this.LoadingData = true;
                this.SetData(scheduleDirections);
                this.LoadingData = false;
            }
            else
            {
                var transportInformaction = await JsonConvert.DeserializeObjectAsync<TransportInformation>(objectForDeserialize);
                this.LoadTransportDirections(transportInformaction);
            }
        }
    }
}
